package org.ouwa.lms.client.ui;

import org.ouwa.lms.client.manager.ManagerRegistry;
import org.ouwa.lms.client.model.Account;
import org.ouwa.lms.client.ui.event.LogoutEvent;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class HeaderPane extends EventPane {
	private final Account account;
	
	public HeaderPane(ManagerRegistry managerRegistry, Account account) {
		super(managerRegistry);
		this.account = account;
		
		Widget leftPanel = getLeftPanel();
		Widget rightPanel = getRightPanel();
		HorizontalPanel mainPanel = new HorizontalPanel();
		mainPanel.setWidth("100%");
		mainPanel.add(leftPanel);
		mainPanel.add(rightPanel);
		mainPanel.setCellHorizontalAlignment(leftPanel, HorizontalPanel.ALIGN_LEFT);
		mainPanel.setCellHorizontalAlignment(rightPanel, HorizontalPanel.ALIGN_RIGHT);
		
		initWidget(mainPanel);
	}
	
	private Widget getLeftPanel() {
		String name = account.getName();
		String message = "Hi, " + name;
		Label messageLabel = new Label(message);
		
		HorizontalPanel leftPanel = new HorizontalPanel();
		leftPanel.setSpacing(10);
		leftPanel.add(messageLabel);
		
		return leftPanel;
	}
	
	private Widget getRightPanel() {
		HorizontalPanel rightPanel = new HorizontalPanel();
		rightPanel.setSpacing(10);
		Anchor aboutLink = new Anchor("About");
		aboutLink.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				
			}
		});
		rightPanel.add(aboutLink);
		rightPanel.add(new Label("|"));
		
		Anchor helpLink = new Anchor("Help");
		helpLink.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				
			}
		});
		rightPanel.add(helpLink);
		rightPanel.add(new Label("|"));
		
		Anchor logoutLink = new Anchor("Log out");
		logoutLink.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				handleLogout();
			}
		});
		rightPanel.add(logoutLink);
		
		return rightPanel;
	}
	
	private void handleLogout() {
		String name = account.getName();
		fireEvent(new LogoutEvent(this, name));
	}
}
